//
//  MainTabbar.swift
//  新浪微博(day01练习)
//
//  Created by KeY on 16/1/11.
//  Copyright © 2016年 KeY. All rights reserved.
//

import UIKit

class MainTabbar: UITabBar {
    
    override init(frame:CGRect){
        super.init(frame: frame)
        addSubview(plusBtn)
       
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //重写父类的layoutSubviews
    override func layoutSubviews() {
        //注意,必须调用父类的layoutSubviews
        super.layoutSubviews()
        //计算单个Tabbar 宽度
        let w = self.bounds.width / 5
        //计算高度
        let h = self.bounds.height
        
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        var index :CGFloat = 0
        for subView in subviews{
        //获取UITabBarButton类型的对像
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                
                    subView.frame = CGRectOffset(rect, index * w, 0)
                //根据索引空出加号按钮位置
                    index += (index == 1 ? 2 : 1)
            }
        
        }
        //加号按钮位置设置
        plusBtn.frame = CGRectOffset(rect, 2 * w, 0)
    }
    lazy var plusBtn: UIButton = {
    
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.sizeToFit()
        return btn
    
    }()

    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func drawRect(rect: CGRect) {
        // Drawing code
    }
    */

}
